package com.hxkj.service.article.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.client.service.ColumnServiceClient;
import com.hxkj.client.service.NavigationServiceClient;
import com.hxkj.domain.enums.ColumnExceptionEnum;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Column;
import com.hxkj.domain.po.ColumnTree;
import com.hxkj.domain.po.LabelManagement;
import com.hxkj.service.article.ColumnService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ColumnServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 12:00
 * @Version 1.0
 */

@Service
@Slf4j
public class ColumnServiceImpl implements ColumnService {


    @Autowired
    private ColumnServiceClient columnServiceClient;

    @Autowired
    private ArticleServiceClient articleServiceClient;

    @Override
    public Column saveColumn(Column column) throws Exception{
        if(column == null){
            throw new CustomMessageException(401,"没有数据需要保存");
        }
        if (column==null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
        }
        if (StringUtils.isBlank(column.getColumnName())){
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_NAME);
        }
        if (StringUtils.isBlank(column.getAlias())){
            throw new CustomMessageException(401,"分类别名不能为空");
        }
        if (column.getSiteId()==null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
        }
        if(column.getTempId()==null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_TEMPID_NOT_NULL);
        }
        if(column.getTempletId()==null){
            throw new CustomException(ColumnExceptionEnum.COLUMN_TEMPLTEID_NOT_NULL);
        }

        ResultDataStruct<Column> resultDataStruct = columnServiceClient.saveColumn(column);
        if(resultDataStruct.getCode()!=200){
            throw new CustomException(ColumnExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
        }
        return resultDataStruct.getData();
    }

    @Override
    public List<ColumnTree> getColumnTreeList(Column column) throws Exception{
        List<ColumnTree> columnTreeList = new ArrayList<>();
        try {
            //Mr.zheng:判断是否是关键字搜索
            if ( StringUtils.isNotBlank(column.getKeyword())){
                columnTreeList =  columnServiceClient.getColumnTreeListByKeyword(column);
            } else {
                columnTreeList =  columnServiceClient.getColumnTreeList(column);
            }

        } catch (Exception e) {
            e.printStackTrace();
           log.info("获取文章分类失败");
        }
        return columnTreeList;
    }

    @Override
    public void removeColumn(Column column) throws Exception {
        ResultMessageStruct resultMessageStruct;
        try {
            if (column == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
            }
            if (StringUtils.isEmpty(column.getColumnIds())) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
            }
            resultMessageStruct = columnServiceClient.removeColumn(column);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(ColumnExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();

            throw e;
        }
    }

    @Override
    public void modifyColumn(JSONObject jsonObject) throws Exception {
        ResultMessageStruct resultMessageStruct;
        if(jsonObject == null){
            throw new CustomMessageException(401,"没有数据需要保存");
        }
        Column column = JSONObject.toJavaObject(jsonObject, Column.class);
        try {
            if (column == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
            }
            if (column.getColumnId() == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_VOID);
            }
            if (column.getSiteId() == null) {
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
            }
            resultMessageStruct = columnServiceClient.modifyColumn(column);

            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(ColumnExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifySortNum(Map<String, Object> map) throws Exception{
        ResultMessageStruct resultMessageStruct;
        try {
            if (map.get("siteId")==null){
                throw new CustomException(ColumnExceptionEnum.COLUMN_NOT_SITEID);
            }
            resultMessageStruct = columnServiceClient.modifySortNum(map);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(ColumnExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<Article> getColumnList(Column column, PageTo pageParam) {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        Article article = new Article();
        article.setColumnId(column.getColumnId());
        article.setSiteId(column.getSiteId());
        article.setAuditType(0);
        article.setDelTag(0);

        ParameterDataStruct<Article> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(article);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<Article> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = articleServiceClient.getArticleList(parameterDataStruct);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章列表失败");
        }
        return resultPageStruct;
    }

    @Override
    public List<ColumnTree> getShowColumnTreeList(Column column) {
        List<ColumnTree> columnTreeList = new ArrayList<>();
        try {
            columnTreeList =  columnServiceClient.getShowColumnTreeList(column);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章分类失败");
        }
        return columnTreeList;
    }

    @Override
    public List<Column> getChildren(Column column) {
        List<Column> columnList = new ArrayList<>();
        try {
           columnList =  columnServiceClient.getChildren(column);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章父类下的子类错误");
        }
        return columnList;
    }

    @Override
    public List<Column> getColumnList(Column column) throws Exception{
        ResultDataStruct<List<Column>> listResultDataStruct;
        try{
            listResultDataStruct = columnServiceClient.getColumnList(column);
            if(listResultDataStruct.getCode() != null){
                throw new CustomException(ColumnExceptionEnum.getEnumByCode(listResultDataStruct.getCode()));
            }
            return listResultDataStruct.getData();
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<Column> getTopParentColumn(Column column) throws Exception {
        return columnServiceClient.getTopParentColumn(column);
    }

}
